package com.nebula.filter;

import com.nebula.common.Const;
import com.nebula.vo.UserVo;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
 * @author CcccX
 * @create 2019-01-14 9:51
 */
@WebFilter(filterName = "LoginFilter")
public class LoginFilter implements Filter {
    private String sessionKey;
    private String redirectUrl;
    private String uncheckedUrls;

    @Override
    public void destroy() {
    }

    @Override
    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) resp;

        String servletPath = request.getServletPath();
        List<String> urls = Arrays.asList(uncheckedUrls.split(","));
        if ("/manage/security".equals(servletPath)) {
            String method = req.getParameter("method");
            if (StringUtils.isNotBlank(method) && "findAll".equals(method)) {
                chain.doFilter(request, response);
                return;
            }
        }
        //放行页面和注册时绑定安全问题
        if ("/question".equals(servletPath)) {
            String method = req.getParameter("method");
            if (StringUtils.isNotBlank(method)) {
                if ("register".equals(method) || "index".equals(method)) {
                    chain.doFilter(request, response);
                    return;
                }
            }
        }

        //放行用户登录、注册的method
        if ("/user".equals(servletPath)) {
            String method = req.getParameter("method");
            if (StringUtils.isNotBlank(method)) {
                if ("register".equals(method) || "login".equals(method)) {
                    chain.doFilter(request, response);
                    return;
                }
            }
        }

        String[] temp = {".js", ".css", ".ico", ".jpg", ".png"};
        if (urls.contains(servletPath)) {
            chain.doFilter(request, response);
            return;
        }

        for (String str : temp) {
            if (servletPath.endsWith(str)) {
                chain.doFilter(request, response);
                return;
            }
        }

        UserVo user = (UserVo) request.getSession().getAttribute(sessionKey);
        if (user == null) {
            response.sendRedirect(request.getContextPath() + redirectUrl);
            return;
        }
        chain.doFilter(request, response);
    }

    @Override
    public void init(FilterConfig config) throws ServletException {
        ServletContext servletContext = config.getServletContext();
        sessionKey = Const.USER_INFO;
        redirectUrl = servletContext.getInitParameter("redirectPage");
        uncheckedUrls = servletContext.getInitParameter("uncheckedUrls");
    }

}
